/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using DaveSexton.DocProject.Sandcastle;

namespace DaveSexton.DocProject.DeploymentSandcastle
{
	public sealed class DeploymentContext
	{
		#region Public Properties
		public IDocProject Project
		{
			get
			{
				return project;
			}
		}

		public string ProjectDirectory
		{
			get
			{
				return projectDirectory;
			}
		}

		public string SourceDirectory
		{
			get
			{
				return sourceDirectory;
			}
		}

		public string TargetDirectory
		{
			get
			{
				return targetDirectory;
			}
		}

		public string RootTargetDirectory
		{
			get
			{
				return rootTargetDirectory;
			}
		}

		public SandcastleSettings Settings
		{
			get
			{
				return settings;
			}
		}

		public DeploymentSandcastleProjectOptions Options
		{
			get
			{
				return options;
			}
		}

		public bool DeployDocSiteContentOnly
		{
			get
			{
				return deployDocSiteContentOnly;
			}
			set
			{
				deployDocSiteContentOnly = value;
			}
		}

		public bool PartialBuildItemsOnly
		{
			get
			{
				return partialBuildItemsOnly;
			}
			set
			{
				partialBuildItemsOnly = value;
			}
		}
		#endregion

		#region Private / Protected
		private readonly DeploymentSandcastleProjectOptions options;
		private readonly SandcastleSettings settings;
		private readonly IDocProject project;
		private readonly string projectDirectory, sourceDirectory, targetDirectory, rootTargetDirectory;
		private bool deployDocSiteContentOnly, partialBuildItemsOnly;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DeploymentContext" /> class.
		/// </summary>
		public DeploymentContext(DeploymentSandcastleBuildEngine engine, Uri target)
		{
			if (engine == null)
				throw new ArgumentNullException("engine");

			if (target == null)
				throw new ArgumentNullException("target");

			options = options = engine.Options;
			project = engine.Project;
			settings = engine.Settings;
			projectDirectory = settings.ProjectDirectory;
			sourceDirectory = settings.TargetDirectory;

			if (!target.IsAbsoluteUri)
				target = new Uri(Path.Combine(projectDirectory, System.Web.HttpUtility.UrlDecode(target.ToString())));

			rootTargetDirectory = PathFormat.EnsureDirectory(
				(target.IsFile || target.IsUnc)
					? System.Web.HttpUtility.UrlDecode(target.AbsolutePath)
					: target.GetLeftPart(UriPartial.Path)
				);

			targetDirectory = Path.Combine(rootTargetDirectory, settings.RelativeTargetDirectory);
		}
		#endregion
	}
}
